/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.item.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.common.annotation.ServiceLog;
import org.springblade.common.constant.TrainingOperateConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.modules.item.entity.ItemEntity;
import org.springblade.modules.item.service.IItemService;
import org.springblade.modules.personal.entity.UserBaseEntity;
import org.springblade.modules.personal.service.IUserBaseService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.item.entity.ItemStockLogEntity;
import org.springblade.modules.item.vo.ItemStockLogVO;
import org.springblade.modules.item.wrapper.StockLogWrapper;
import org.springblade.modules.item.service.IStockLogService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 * 教具出入库日志 控制器
 *
 * @author
 * @since 2023-03-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-stockLog/stockLog")
@Api(value = "教具出入库日志", tags = "教具出入库日志接口")
public class StockLogController extends BladeController {

	private final IStockLogService stockLogService;
	private final IUserBaseService userBaseService;
	private final IItemService iItemService;

	/**
	 * 教具出入库日志 详情
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入stockLog")
	public R<ItemStockLogVO> detail(ItemStockLogEntity stockLog) {
		ItemStockLogEntity detail = stockLogService.getOne(Condition.getQueryWrapper(stockLog));
		return R.data(StockLogWrapper.build().entityVO(detail));
	}

	/**
	 * 教具出入库日志 分页
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入stockLog")
	public R<IPage<ItemStockLogVO>> list(ItemStockLogVO stockLog, Query query) {
		LambdaQueryWrapper<ItemStockLogEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		if (ObjectUtil.isNotEmpty(stockLog.getItemId())) {
			lambdaQueryWrapper.eq(ItemStockLogEntity::getItemId, stockLog.getItemId());
		}
		if (ObjectUtil.isNotEmpty(stockLog.getType())) {
			lambdaQueryWrapper.eq(ItemStockLogEntity::getType, stockLog.getType());
		}

		if (ObjectUtil.isNotEmpty(stockLog.getStartTime())) {//有开始时间  大于等于
			lambdaQueryWrapper.ge(ItemStockLogEntity::getCreateTime, stockLog.getStartTime());
		}
		if (ObjectUtil.isNotEmpty(stockLog.getEndTime())) {//有开始时间  小于等于
			lambdaQueryWrapper.le(ItemStockLogEntity::getCreateTime, stockLog.getEndTime());
		}

		IPage<ItemStockLogEntity> pages = stockLogService.page(Condition.getPage(query), lambdaQueryWrapper);
		IPage<ItemStockLogVO> itemStockLogVOIPage = StockLogWrapper.build().pageVO(pages);
		itemStockLogVOIPage.getRecords().forEach(itemStockLogVO -> {
			UserBaseEntity userBaseEntity = userBaseService.selectByUserId(itemStockLogVO.getCreateUser());
			if (null == userBaseEntity) return;
			ItemEntity item = iItemService.getById(itemStockLogVO.getItemId());
			if (null != item) itemStockLogVO.setItemName(item.getItemName());
			itemStockLogVO.setUserName(userBaseEntity.getUserName());
			itemStockLogVO.setIdCard(userBaseEntity.getUserNumber());
		});

		return R.data(itemStockLogVOIPage);
	}


	/**
	 * 教具出入库日志 新增
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@ServiceLog(value = TrainingOperateConstant.ITEM, operate = TrainingOperateConstant.ADD, describe = "新增教具")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入stockLog")
	public R save(@Valid @RequestBody ItemStockLogEntity stockLog) {
		return R.status(stockLogService.save(stockLog));
	}

	/**
	 * 教具出入库日志 修改
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@ServiceLog(value = TrainingOperateConstant.ITEM, operate = TrainingOperateConstant.UPDATE, describe = "修改教具")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入stockLog")
	public R update(@Valid @RequestBody ItemStockLogEntity stockLog) {
		return R.status(stockLogService.updateById(stockLog));
	}


	/**
	 * 教具出入库日志 删除
	 */
	//@PreAuth(TrainingRoleConstant.HAS_ROLE_TEACHER)
	@ServiceLog(value = TrainingOperateConstant.ITEM, operate = TrainingOperateConstant.DELETE, describe = "教具删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(stockLogService.deleteLogic(Func.toLongList(ids)));
	}

}
